/**
 *
 */
package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.OperationLogMapper;
import com.ybkj.daijia.server.model.OperationLog;
import com.ybkj.daijia.server.service.OperationLogService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Shine
 *
 */
@Service
public class DefaultOperationLogService implements OperationLogService {

    @Autowired
    private OperationLogMapper operationLogMapper;


    /* (non-Javadoc)
     * @see com.ybkj.shop.service.AdminService#queryAll()
     */
    public Page<OperationLog> queryAll(String operating, Date start, Date end) {

        String account = "shine";

        Subject currentUser = SecurityUtils.getSubject();
        Principal principal = (Principal) currentUser.getPrincipal();

        Map<String, Object> params = new HashMap<String, Object>();

        if (!principal.isCompanyType()) {
            params.put("compamyId", principal.getCompanyId());
        }
        if (!principal.getUsername().equals(account)) {
            params.put("neoperating", account);
        }
        if (StringUtils.isNotBlank(operating)) {
            params.put("operating", "%" + operating + "%");
        }
        if (null != start) {
            params.put("start", start);
        }
        if (null != end) {
            params.put("end", end);
        }

        params.put("offset", Pager.getOffset());
        params.put("size", Pager.getLimit());

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        List<OperationLog> content = operationLogMapper.selectByMap(params);
        Long total = operationLogMapper.countByMap(params);
        return new PageImpl<OperationLog>(content, pageable, total);

    }

    @Transactional(rollbackFor = Exception.class)
    public OperationLog insertRecord(OperationLog operationLog) {

        operationLogMapper.insertRecord(operationLog);

        return operationLog;
    }

    public OperationLog findOne(Long id) {

        return operationLogMapper.selectByPrimaryKey(id);
    }


    @Transactional(rollbackFor = Exception.class)
    public void delete(OperationLog operationLog) {

        operationLogMapper.deleteByPrimaryKey(operationLog.getId());

    }

    @Override
    public List<OperationLog> export(HttpSession session, String operating, Date start, Date end,
        Long timestamp) {

        String account = "shine";

        Subject currentUser = SecurityUtils.getSubject();
        Principal principal = (Principal) currentUser.getPrincipal();

        Map<String, Object> params = new HashMap<String, Object>();

        if (!principal.isCompanyType()) {
            params.put("compamyId", principal.getCompanyId());
        }
        if (!principal.getUsername().equals(account)) {
            params.put("neoperating", account);
        }
        if (StringUtils.isNotBlank(operating)) {
            params.put("operating", "%" + operating + "%");
        }
        if (null != start) {
            params.put("start", start);
        }
        if (null != end) {
            params.put("end", end);
        }

        Long total = operationLogMapper.countByMap(params);
        List<OperationLog> totalContent = new ArrayList<OperationLog>();

        Long forTimes = total / 1000;
        if (total % 1000 != 0) {
            ++forTimes;
        }

        for (int i = 0; i < forTimes; i++) {

            params.put("offset", i * 1000);
            params.put("size", 1000);
            List<OperationLog> content = operationLogMapper.selectByMap(params);
            totalContent.addAll(content);

            // 进度
            session.setAttribute("exportOperationlogProcess_" + timestamp,
                Double.valueOf(i + 1) / Double.valueOf(forTimes));
            // 进度

        }

        // 进度
        session.removeAttribute("exportOperationlogProcess_" + timestamp);
        // 进度

        return totalContent;
    }

}